<?php
/**
 *
 * @category   JND
 * @package    Jnd_View
 * @subpackage Helper
 * @copyright  Copyright (c) 2005-2009 DDB
 * @version    $Id: FormUploadify.php 16541 2009-10-12 17:02:30Z tbrouard $
 */
/**
 * Abstract class for extension
 */
require_once 'Zend/View/Helper/FormElement.php';
/**
 * Helper to generate a "Uploadify" element
 *
 * @category   JND
 * @package    Jnd_View
 * @subpackage Helper
 */
class Jnd_View_Helper_FormUploadify extends Zend_View_Helper_FormElement
{

    /**
     * Generates a 'hidden' element.
     *
     * @access public
     *
     * @param string|array $name If a string, the element name.  If an
     * array, all other parameters are ignored, and the array elements
     * are extracted in place of added parameters.
     * @param mixed $value The element value.
     * @param array $attribs Attributes for the element tag.
     * @return string The element XHTML. div tag + input hidden tag
     */
    public function formUploadify($name, $value = null, array $attribs = null)
    {
        $info = $this->_getInfo($name, $value, $attribs);
        extract($info); // name, value, attribs, options, listsep, disable
        if (isset($id)) {
            if (isset($attribs) && is_array($attribs)) {
                $attribs['id'] = $id;
            } else {
                $attribs = array('id' => $id);
            }
        } else {
            $attribs['id'] = $name;
        }
        $attribs['name'] = $name;
        // Defaults attribs
        $defaultAttribs = array(
            "uploader" => "uploadify.swf", "script" => "uploadify.php",
            "cancelImg" => "cancel.png", "css" => "uploadify.css",
            "auto" => true, "multi" => true, "width" => 110,
            "height" => 30, "buttonText" => "Parcourir",
            "scriptAccess" => "always", "application" => "",
            "folder" => "", "encode" => "false",
            "onComplete" => "
        function (event, queueID, fileObj, responses) {
          var donnees = $('#displayUploadDone').html();
            $('#displayUploadDone').html( donnees + 'Fichier ' + fileObj.name + ' téléchargé, url : <a target=\"_blank\" href=' + url + fileObj.name + '>' + url + fileObj.name + '</a><br />');
        }",
            "onAllComplete" => "
        function (event, queueID, fileObj, responses) {
          var donnees = $('#displayUploadDone').html();
            $('#displayUploadDone').html( donnees + 'Fichier ' + fileObj.name + ' téléchargé, url : <a target=\"_blank\" href=' + url + fileObj.name + '>' + url + fileObj.name + '</a><br />');
        }");
        $attribsUploadify = array_merge($defaultAttribs, $attribs);
        $view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource(
            "view");
        $jquery = Ddb_App::getConfig('jquery');
        $jqueryServeurUrl = $jquery['path']['min'];
        $view->jQuery()->addJavascriptFile(
            $jqueryServeurUrl . 'plugins/uploadify/jquery.uploadify.js')->addJavascriptFile(
            $jqueryServeurUrl . 'plugins/uploadify/swfobject.js')->addStylesheet(
            $jqueryServeurUrl . 'plugins/uploadify/' . $attribsUploadify['css']);
        $view->jQuery()->addOnLoad(
            "
      $('#" . $attribsUploadify['id'] . "').uploadify({
        'uploader': '" . $jqueryServeurUrl . "plugins/uploadify/" . $attribsUploadify['uploader'] . "',
        'script': '" . $jqueryServeurUrl . "plugins/uploadify/" . $attribsUploadify['script'] . "',
        'height':" . $attribsUploadify['height'] . ",
        'width': " . $attribsUploadify['width'] . ",
        'cancelImg' : '" . $jqueryServeurUrl . "plugins/uploadify/" . $attribsUploadify['cancelImg'] . "',
                'auto'      : " . $attribsUploadify['auto'] . ",
                'multi'      : " . $attribsUploadify['multi'] . ",
                'buttonText'      : '" . $attribsUploadify['buttonText'] . "',
                'scriptAccess'      : '" . $attribsUploadify['scriptAccess'] . "',
                'application'      : '" . $attribsUploadify['application'] . "',
                'folder'    : '" . $attribsUploadify['folder'] . "',
                'onComplete'    : " . $attribsUploadify['onComplete'] . ",
                'onAllComplete'    : " . $attribsUploadify['onAllComplete'] . "
      });");
        $helper = new Zend_View_Helper_FormFile();
        $helper->setView($view);
        //  On supprime ce qui n'est pas html
        $aSupprimer = array('uploader', 'script',
            'cancelImg', 'auto', 'multi', 'buttonText', 'scriptAccess',
            'application', 'onComplete', 'onAllComplete', 'folder');
        foreach ($aSupprimer as $aEff) {
            unset($attribs[$aEff]);
        }
        return $helper->formFile($attribs['id'], $attribs);
    }
}
